from variable import *

def copy_eightATPreports():
    #       3.1.1 拷贝8分成功检测文件：
    # MAGCube，MAGCloudAndRelative，ECGPacing，ECGNoise，
    # ECGLimb，ECGAmplitude，ACLCalibration，ACLRawData
    os.chdir(MailWork_path)

    reportKey = ["ACLCalibration", "ACLRawData", "ECGAmplitude", "ECGLimb",
                 "ECGNoise", "ECGPacing", "MAGCloudAndRelative", "MAGCube"]

    file_dict = {"ACLCalibration": [0], "ACLRawData": [0], "ECGAmplitude": [0],
                 "ECGLimb": [0], "ECGNoise": [0], "ECGPacing": [0], "MAGCloudAndRelative": [0],
                 "MAGCube": [0]
                 }
    AllReportFile_list = []  # 存储ServiceReports所有pdf
    ATPReportFileName_list = []  # 存储最新的8份ATP Report
    test_list = []  # 存储ServiceReports的分离文件名

    if not os.path.exists(ATP1Folder_path):
        print(ATP1Folder_path + "not exist,please create the dir")

    # 获取文件名列表
    if os.path.exists(ATPReport_path):
        print(ATPReport_path + " exitst")
        for root, dirs, files in os.walk(ATPReport_path):
            if root == ATPReport_path:  # 限定在ATPReport_path下遍历文件
                for key in reportKey:
                    for file in files:
                        if key in file:
                            # print('找到包含"' + key + '"字符的文件,绝对路径为----->' + ATPReport_path + '/' + file)
                            # print('复制到----->' + ATP1Folder_path + "/" + file)
                            AllReportFile_list.append(file)
    # 文件名分离
    if not os.path.isfile("a.txt"):
        fd = open("a.txt", mode="a", encoding="utf-8")
        for filename in AllReportFile_list:
            fd.write(filename + '\n')
            test_list.append([''.join(list(g)) for k, g in groupby(filename, key=lambda x: x.isdigit())])
        fd.close()
    if not os.path.isfile("获取文件名列表.txt"):
        os.rename("a.txt", "获取文件名列表.txt")

    # 报告文件数字转入字典
    for i in test_list:
        for key, value in file_dict.items():
            if key == i[0]:
                value.append(i[1])

    # 字典写入Json验证
    jsObj = json.dumps(file_dict)
    fileObject = open(r'test.json', 'a')
    fileObject.write(jsObj)
    fileObject.close()

    # 获取最新报告文件
    for key, value in file_dict.items():
        numbers = [int(x) for x in file_dict[key]]
        print(key + str(max(numbers)) + ".pdf")
        ATPReportFileName_list.append(key + str(max(numbers)))
    print(ATPReportFileName_list)

    # 拷贝最终报告
    if os.path.exists(ATPReport_path):
        print(ATPReport_path + " exitst")
        for root, dirs, files in os.walk(ATPReport_path):
            if root == ATPReport_path:  # 限定在ATPReport_path下遍历文件
                for key in ATPReportFileName_list:
                    for file in files:
                        if key in file:
                            # print('找到包含"' + key + '"字符的文件,绝对路径为----->' + ATPReport_path + '/' + file)
                            # print('复制到----->' + MailWork_path + "/" + file)
                            src_file = os.path.join(root, file)
                            shutil.copy(src_file, MailWork_path)
    pass